﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SFR
{
    public partial class Main : Form
    {
        MongoDbHelper mongo;
        Dictionary<string,bool> TagsPerDocument;

        public Main()
        {
            InitializeComponent();

            dataGridView1.AutoGenerateColumns = false;

            try
            {
                mongo = new MongoDbHelper();

                //ListDb();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Error: "+e.Message); return;
            }

            TagsPerDocument = new Dictionary<string,bool>();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            Import imf = new Import(mongo);
            imf.ImportCompleted += new Import.ImportEventHandler(imf_ImportCompleted);
            imf.Show();
        }

        void imf_ImportCompleted(object sender, EventArgs e)
        {
            ListDb();
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            var list = from DataGridViewRow i in dataGridView1.Rows
                       let cell = (DataGridViewCheckBoxCell)i.Cells[0]
                       where (cell.Value != null) && (bool)cell.Value == true 
                       select (Doc)i.DataBoundItem;

            Export exf = new Export(mongo, list.ToArray());
            exf.ShowDialog();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            var list = from DataGridViewRow i in dataGridView1.Rows
                       let cell = (DataGridViewCheckBoxCell)i.Cells[0]
                       where (cell.Value != null) && (bool)cell.Value == true 
                       select (Doc)i.DataBoundItem;

            if (list.ToArray().Length == 0)
            {
                System.Windows.Forms.MessageBox.Show("No files marked for deletion! Use the checkboxes.");
                return;
            }

            foreach (Doc item in list.ToArray())
            {
                mongo.Delete(item);
            }

            ListDb();
        }

        private void dropDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mongo.DropDb();
        }

        public void ListDb()
        {
            dataGridView1.DataSource = mongo.GetAll();
            dataGridView1.ClearSelection();
        }

        private void buttonSelectAll_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewRow i in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell cbox = (DataGridViewCheckBoxCell)i.Cells[0];
                cbox.Value = true;
            }
                      
        }

        private void buttonUnselectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow i in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell cbox = (DataGridViewCheckBoxCell)i.Cells[0];
                cbox.Value = false;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ListDb();
        }

        private void buttonSearchTags_Click(object sender, EventArgs e)
        {
            //var dbEntries = mongo.GetAll();

            Dictionary<string,bool> selectedTags = GetSelectedTags(treeView1);

            ////Doc[] dbEntries = (from entry in _documents.FindAll() select (Doc)entry).ToArray();

            //var p = from d in dbEntries
            //        where (selectedTags.Count > 0 && d.Tags.ToList<string>().Count > 0 && (d.Tags.ToList<string>()).Except(selectedTags).ToList().Count == 0)
            //              || (selectedTags.Count == 0 && d.Tags.ToList<string>().Count == 0)

            //        select d;

           // BsonDocument[] dbEntries = mongo.GetAll();

            if (selectedTags.Count == 0 && this.textBoxSearch.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("No search criteris(s) defined!");
                return;
            }

           dataGridView1.DataSource = mongo.Search(selectedTags,this.textBoxSearch.Text);
           dataGridView1.ClearSelection();
        }

        public Dictionary<string, bool> GetSelectedTags(TreeView tree)
        {
            TagsPerDocument.Clear();
            ListNodes(tree.Nodes[0]);
            return TagsPerDocument;
        }

        void ListNodes(TreeNode node)
        {
            if (node.Checked)
                TagsPerDocument.Add(node.Level + "->" + node.Name,true);

            foreach (TreeNode subnode in node.Nodes)
            {
                ListNodes(subnode);
            }
            // Print out node
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void clearAllEntriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mongo.ClearAllEntries();

            ListDb();
        }

        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login lf = new Login(mongo);

            lf.Show();
        }
    }
}
